package com.danger.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;
import com.danger.Activity.R;
import com.danger.Model.ListViewBean;
import com.danger.Widget.MyCircleImageView;

import java.util.List;

/**
 * Created by 俊 on 2016/6/10.
 */
public class SwipeListViewAdapter extends BaseSwipeAdapter {

    private Context context;
    private List<ListViewBean> list;

    public SwipeListViewAdapter(Context context, List<ListViewBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipe;
    }

    @Override
    public View generateView(int position, ViewGroup parent) {
        View v = LayoutInflater.from(context).inflate(R.layout.swipe_listview_item, parent, false);
        SwipeLayout swipeLayout = (SwipeLayout) v.findViewById(getSwipeLayoutResourceId(position));
        swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
            @Override
            public void onDoubleClick(SwipeLayout layout, boolean surface) {
                Toast.makeText(context, "DoubleClick", Toast.LENGTH_SHORT).show();
            }
        });
        v.findViewById(R.id.delete_textView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "click delete", Toast.LENGTH_SHORT).show();
            }
        });
        v.findViewById(R.id.top_textView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "click top", Toast.LENGTH_SHORT).show();
            }
        });
        return v;
    }

    @Override
    public void fillValues(int position, View convertView) {
        ViewHolder holder = new ViewHolder(convertView);
        holder.imageView.setImageResource(list.get(position).getPhoto());
        holder.title_textView.setText(list.get(position).getTitle());
        holder.time_textView.setText(list.get(position).getTime());
        holder.message_textView.setText(list.get(position).getMessage());
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {

        public MyCircleImageView imageView;
        public TextView title_textView;
        public TextView time_textView;
        public TextView message_textView;

        public ViewHolder(View view) {
            imageView = (MyCircleImageView) view.findViewById(R.id.imageView);
            title_textView = (TextView) view.findViewById(R.id.title_textView);
            time_textView = (TextView) view.findViewById(R.id.time_textView);
            message_textView = (TextView) view.findViewById(R.id.message_textView);
        }
    }
}